package es.titoapps.spanishhandball.iu.centraldedatos;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;

import com.markupartist.android.widget.PullToRefreshListView;

import es.titoapps.spanishhandball.R;
import es.titoapps.spanishhandball.database.DatabaseUtilities;
import es.titoapps.spanishhandball.entities.TeamClassification;
import es.titoapps.spanishhandball.tools.NetworkTools;
import es.titoapps.spanishhandball.tools.Utilities;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ClasificacionActivity extends Fragment {

	protected ProgressDialog pDialog;
	private ArrayList<TeamClassification> teamClassification;
	
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//		super.onCreate(savedInstanceState);
//		setContentView(R.layout.clasificacionlayout);
		
		return inflater.inflate(R.layout.clasificacionlayout, container, false);
	}
	
	public void onResume (){
		super.onResume();
		
		if(teamClassification==null){
			teamClassification=DatabaseUtilities.getClassification(-1);
			if(teamClassification.size()==0){
				ClasificationLoaderThread thread=new ClasificationLoaderThread();
				thread.execute();
			}else{
				fillInTable();
			}
		}
		
	}
	
	
	private TextView setTextViewWithText(Activity context,String textToWrite){
		
		TextView tv = new TextView(context);
		tv.setText(textToWrite);
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		
		return tv;
	}
	
	private TableRow setHeaders(Activity context){
		
		TableRow header = new TableRow(context);
		
		TextView tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderPosition));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderTeam));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGamesPlayed));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGamesWon));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGamesTied));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGamesLost));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGamesPlayedHome));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGamesWonHome));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGamesTiedHome));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGamesLostHome));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGamesPlayedAway));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGamesWonAway));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGamesTiedAway));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGamesLostAway));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGoalsScored));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGoalsAgainst));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderPointsTotal));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGoalsScoredHome));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGoalsAgainstHome));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderPointsHome));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGoalsScoredAway));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderGoalsAgainstAway));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		tv = new TextView(context);
		tv.setText(context.getString(R.string.tableHeaderPointsAway));
		tv.setGravity(Gravity.LEFT);
		tv.setPadding(5, 5, 5, 5);
		header.addView(tv);
		
		return header;
	
	}
	
	private void fillInTable() {
		
		int mRowCount=0;
		Activity activity=this.getActivity();
		TableLayout table = (TableLayout)activity.findViewById(R.id.tableLayout);
		
		TableRow header = new TableRow(activity);
		
		table.addView(setHeaders(activity));
		
		for (TeamClassification team : teamClassification) {
			
			TableRow row = new TableRow(activity);
			row.addView(setTextViewWithText(activity,Integer.toString(team.getPosition())));
			row.addView(setTextViewWithText(activity,team.getFullName()));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGamesPlayed())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGamesWon())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGamesTied())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGamesLost())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGamesPlayedHome())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGamesWonHome())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGamesTiedHome())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGamesLostHome())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGamesPlayedAway())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGamesWonAway())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGamesTiedAway())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGamesLostAway())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGoalsScored())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGoalsAgainst())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getPointsTotal())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGoalsScoredHome())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGoalsAgainstHome())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getPointsHome())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGoalsScoredAway())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getGoalsAgainstAway())));
			row.addView(setTextViewWithText(activity,Integer.toString(team.getPointsAway())));
			
			table.addView(row);

		}
			
	}
	
	
	class ClasificationLoaderThread extends AsyncTask<String, String, String>{

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(getActivity());

			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.setMessage("Cargando ...");

			pDialog.show();
		}

		
		@Override
		protected String doInBackground(String... params) {
			try {
				teamClassification=NetworkTools.getClasificationHtml();
				DatabaseUtilities.insertClassification(teamClassification);
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		
		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			
			if(teamClassification.size()!=0)
				fillInTable();
			
			pDialog.dismiss();
			
		}
	}

}
